﻿Option Explicit On
Option Strict On
Imports System.Data.SqlClient

Public Class frmAporte

    Dim objAporte As New clsAporte
    Dim Operacion As String = ""
    Dim Legajo As Integer

    ' Cadena de conexión  
    Private Const cadenaconexion As String = "Data Source= .\SQLEXPRESS; Integrated Security=True; Initial Catalog=ficticia"

    Public Sub SetEmpleado(ByVal T_Legajo As Integer)
        Legajo = T_Legajo
    End Sub

    Private Sub frmAporte_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        sub_HabilitarControles(False)
        sub_RefreshAporte()
    End Sub

    Private Sub sub_RefreshAporte()
        With objAporte
            .pLegajo = Legajo
            If .fn_EjecutarQuery(clsAporte.TipoQuery.InfoTodo) Then
                Grid.DataSource = .dbAporDataSet.Tables("Aportes")

            Else
                MessageBox.Show(.pMsgAporte)
            End If
        End With
    End Sub

    Private Sub sub_HabilitarControles(ByVal Valor As Boolean)
        GrpListado.Enabled = Not Valor
        GrpIndividual.Enabled = Valor

        If GrpListado.Enabled Then
            Me.Height = 293
        Else
            Me.Height = 503
        End If

    End Sub

    Private Sub cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelar.Click
        sub_HabilitarControles(False)
        Nuevo.Focus()
    End Sub



    Private Sub Eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Eliminar.Click
        If Grid.RowCount <= 0 Then Exit Sub
        If Grid.SelectedRows.Count = 1 Then
            Operacion = "Del"
            If MessageBox.Show("¿Esta seguro de eliminar el aporte con codigo " & CInt(Grid.SelectedRows(0).Cells("Codigo").Value), "Confirmación", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                aceptar_Click(aceptar, New System.EventArgs)
            End If
        Else
            MessageBox.Show("Seleccione una Fila, No una celda o columna")
            sub_HabilitarControles(False)
        End If
    End Sub



    Private Sub Nuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Nuevo.Click
        aceptar.Text = "Guardar Nuevo"
        Operacion = "Add"
        sub_HabilitarControles(True)
        Codigo.ReadOnly = False
        Valor.Focus()
    End Sub

    Private Sub Modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Modificar.Click
        If Grid.RowCount <= 0 Then Exit Sub

        aceptar.Text = "Guardar Modificación"
        Operacion = "Upd"

        With objAporte
            If Grid.SelectedRows.Count = 1 Then
                sub_HabilitarControles(True)
                .pCodigo = CInt(Grid.SelectedRows(0).Cells("Codigo").Value.ToString)
                .pNombre = CStr((Grid.SelectedRows(0).Cells("Nombre").Value))
                .pValor = CInt(Grid.SelectedRows(0).Cells("Valor").Value)


                If .fn_EjecutarQuery(clsAporte.TipoQuery.InfoIndividual) Then
                    Codigo.Text = .dbAporDataRow("Codigo").ToString
                    Codigo.ReadOnly = True
                    Valor.Text = .dbAporDataRow("Valor").ToString
                    Nombre.Text = .dbAporDataRow("Nombre").ToString
                    Valor.Focus()
                Else
                    MessageBox.Show(.pMsgAporte)

                End If
            Else
                MessageBox.Show("Seleccione la Fila, No una celda o columna")
                sub_HabilitarControles(False)
            End If


        End With
    End Sub

    Private Sub aceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles aceptar.Click
        With objAporte
            Select Case Operacion
                Case "Add"

                    If Trim(Codigo.Text) = "" Then
                        .pCodigo = 0
                    Else
                        .pCodigo = CInt(Trim(Codigo.Text))
                    End If

                    .pNombre = Trim(Nombre.Text)

                    If Trim(Valor.Text) = "" Then
                        .pValor = 0
                    Else
                        .pValor = CInt(Trim(Valor.Text))
                    End If

                    If Not .fn_ExisteAporte(clsAporte.VerifExistType.Ambos) Then
                        If .fn_AddAporte() Then
                            sub_RefreshAporte()
                            sub_HabilitarControles(False)
                        Else
                            MessageBox.Show(.pMsgAporte)
                        End If
                    Else
                        MessageBox.Show("El Aporte ya Existe.")
                    End If

                Case "Upd"


                    .pCodigo = CInt(Trim(Codigo.Text))
                    .pNombre = Trim(Nombre.Text)

                    If Trim(Valor.Text) = "" Then
                        .pValor = 0
                    Else
                        .pValor = CInt(Trim(Valor.Text))
                    End If

                    If .fn_UpdAporte() Then
                        sub_RefreshAporte()
                        sub_HabilitarControles(False)

                    Else
                        MessageBox.Show(.pMsgAporte)
                    End If

                Case "Del"
                    .pCodigo = CInt(Grid.SelectedRows(0).Cells("Codigo").Value)
                    If .fn_DelAporte() Then
                        sub_RefreshAporte()
                    Else
                        MessageBox.Show(.pMsgAporte)
                    End If
            End Select
        End With
    End Sub
End Class